//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}
//验证token
var vid = ""
var vimg = ""
var bol = false
$.ajax({
    type:"POST",
    url:"/api/admin/find",
    data:{
        id:getCookie("token"),
    },
    success:data=>{
        console.log(data)
        vid = data.data.id
        vimg = data.data.img
        if(data.data.data==="no token！"){
            $(".close").hide()
            console.log("No details yet！！！")
        }else{
            bol = true
            $(".toux").attr({src:""+data.data.img+""})
            $(".close").show()
        }
    },
})
//删除cookie
function clearCookie(name) {
    setCookie(name,"", -1);
}
//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";//path=/是根路径
}
//退出登录
$('.close').on("click",()=>{
    clearCookie("token")
    window.location.reload()
})

//换头像
$(".toux").on("click",()=>{
    if(bol){
        $(".huantx").slideToggle()
    }else{
        console.log("没有cookie")
    }
})
$(".huantx img").on("click",(e)=>{
    const a = $(e.target).attr("src")
    $.ajax({
        type:"POST",
        url:"/api/admin/update",
        data:{
            id:vid,
            img:`http://localhost:3300/${a}`,
        },
        success:(data)=>{
            window.location.reload()
        }
    })
})
//
$(".suretx").on("click",()=>{
    const img= $(".wj")[0].files
     //构建formdata格式的参数                            
     const formData = new FormData()
     for (const file of img) {
         formData.append("img",file,file.name)
     }
    console.log(formData)
    if(formData.length==0){
        alert("未上传图片")
    }else{
        $.ajax({
                type:"POST",
                url:"/api/upload",
                data:formData,
                contentType: false,
                // 告诉jQuery不要去设置Content-Type请求头
                processData: false,
                // 告诉jQuery不要去处理发送的数据
                success:(data)=>{
                    console.log(data.data)
                }
            })
    }
})
//热度排行
$.ajax({
    type: "GET",
    url: "/api/getboke/gethot",
    success:(data)=>{
        const obj = data.data
        console.log(obj)
        $("#hot1").html(obj[0].text).parent().next().children().html(`阅读量${obj[0].hot}`)
        $("#hot1").attr("href",`http://localhost:3300/api/getboke/getfindbk?id=${obj[0].id}`)
        $("#hot2").html(obj[1].text).parent().next().children().html(`阅读量${obj[1].hot}`)
        $("#hot2").attr("href",`http://localhost:3300/api/getboke/getfindbk?id=${obj[1].id}`)
        $("#hot3").html(obj[2].text).parent().next().children().html(`阅读量${obj[2].hot}`)
        $("#hot3").attr("href",`http://localhost:3300/api/getboke/getfindbk?id=${obj[2].id}`)
    }
})
//分类
function Fl(text,flag){
    if(flag){
        $.ajax({
            type: "GET",
            url: "/api/getboke/getfl",
            data:{lx:text.text()},
            success:(data)=>{
                const obj = data.data.rows
                $("#bk").html("")
                for (let i = 0; i < obj.length; i++){
                    $("#bk").append(`
                    <div class="col-md-12 col-sm-6">
                    <!-- post -->
                    <div class="post post-list clearfix">
                        <div class="thumb rounded">
                            <span class="post-format-sm">
                                <i class="icon-picture"></i>
                            </span>
                            <a href="http://localhost:3300/api/getboke/getfindbk?id=${obj[i].id}">
                                <div class="inner">
                                    <img src=${obj[i].img} alt="post-title" />
                                </div>
                            </a>
                        </div>
                        <div class="details">
                            <ul class="meta list-inline mb-3">
                                <li class="list-inline-item"><a href="#"><img src="images/other/author-sm.png" class="author" alt="author"/>Han Yu</a></li>
                                <li class="list-inline-item"><a href="#">${obj[i].hot}阅读</a></li>
                                <li class="list-inline-item">${obj[i].createdAt.slice(0,10)}</li>
                            </ul>
                            <h5 class="post-title"><a href="http://localhost:3300/api/getboke/getfindbk?id=${obj[i].id}">${obj[i].name}</a></h5>
                            <p class="excerpt mb-0">${obj[i].text}</p>
                            <div class="post-bottom clearfix d-flex align-items-center">
                                <div class="social-share me-auto">
                                    <button class="toggle-button icon-share"></button>
                                </div>
                                <div class="more-button float-end">
                                    <a href="http://localhost:3300/api/getboke/getfindbk?id=${obj[i].id}"><span class="icon-options"></span></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                    `)
                }
                }
            })  
    }else{
        $.ajax({
        type: "GET",
        url: "/api/getboke/getfl",
        data:{lx:text.text()},
        success:(data)=>{
            const obj = data.data.rows
            text.next().html(`(${obj.length})`)
            }   
        })
    }
}
Fl($("#fl1"),false)
Fl($("#fl2"),false)
Fl($("#fl3"),false)
Fl($("#fl4"),false)
Fl($("#fl5"),false)
$("#fl1").on("click",function(){
    Fl($("#fl1"),true)
})
$("#fl2").on("click",function(){
    Fl($("#fl2"),true)
})
$("#fl3").on("click",function(){
    Fl($("#fl3"),true)
})
$("#fl4").on("click",function(){
    Fl($("#fl4"),true)
})
$("#fl5").on("click",function(){
    Fl($("#fl5"),true)
})
//发布文章
$(".fbwz").on("click",function(){
    const name  = $("#bk-bt").val()
    const text = $(".w-e-text").text()
    const img = `http://localhost:3300/images/posts/latest-sm-4.jpg`
    const fabiao = `发布博客1月前`
    const hot = 0
    const dz = `0点赞`
    const pl = `0 评论`
    const txt = hzh
    var lx
    for (i = 0; i < $(".ir").length; i++) {
        if ($(".ir").eq(i).prop("checked")) {
            lx = $(".ir").eq(i).val()
        }
    }
    console.log(lx)
    if(vid==1){
        if(name!=""&&text!=""&&lx!=""){
            $.ajax({
                type: "POST",
                url: "/api/getboke/addbk",
                data:{
                    name,
                    text,
                    img,
                    fabiao,
                    hot,
                    dz,
                    pl,
                    txt,
                    lx,
                },
                success:(data)=>{
                    alert("发布成功")
                    window.location.reload()
                }
            })
        }else{
            alert("不能为空")
        }
    }else{
        alert("无权限")
        window.location.reload()
    }
})
